import 'package:flutter/material.dart';

class HomeList extends StatelessWidget {
  // required 必传校验，父组件使用时会校验，子组件数据不可能为 null
  const HomeList({super.key, required this.notifyList});

  // List notifyList = [];
  final List notifyList; // 类似鸿蒙的 @Link 引用父组件数据，内部不需要定义初始值

  // 头部的结构
  List<Widget> getTitleList() {
    return [
      Image.asset(
        'assets/images/notice@2x.png',
        width: 25,
      ),
      const Text(
        '社区',
        style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
      const Text(
        '公告',
        style: TextStyle(
            fontSize: 16, fontWeight: FontWeight.bold, color: Colors.orange),
      ),
    ];
  }

  List<Widget> getListChildren(BuildContext context) {
    return notifyList.map(
      (item) {
        return GestureDetector(
          onTap: () {
            // 跳转公告详情页
            Navigator.pushNamed(
              context,
              '/notice_detail',
              arguments: item['id'], // 传递 id 参数给详情页
            );
          },
          child: Container(
            padding: const EdgeInsets.only(top: 10, bottom: 10),
            child: Container(
              padding: const EdgeInsets.all(10),
              margin: const EdgeInsets.only(top: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          item['title'],
                          style: const TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 8,
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          item['content'],
                          maxLines: 3,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 8),
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          item['createdAt'],
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: const TextStyle(color: Colors.grey),
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      },
    ).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 10),
      child: Column(
        children: [
          Row(
            children: getTitleList(), // 头部
          ),
          ListView(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            children: getListChildren(context), // 底部内容
          )
        ],
      ),
    );
  }
}
